package com.egoo.ticket.server.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.dao.FilterFirstDao;
import com.egoo.ticket.server.dao.FilterSecondDao;
import com.egoo.ticket.server.pojo.dto.FilterSecondOutDto;
import com.egoo.ticket.server.pojo.entity.FilterFirst;
import com.egoo.ticket.server.pojo.entity.FilterSecond;
import com.egoo.ticket.server.pojo.vo.FilterDataVo;
import com.egoo.ticket.server.pojo.vo.TypeVo;
import com.egoo.ticket.server.service.FilterFirstService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class FilterFirstServiceImpl implements FilterFirstService {

	@Autowired
	private FilterFirstDao filterFirstDao;
	@Autowired
	private FilterSecondDao filterSecondDao;

	/**
	 * TODO 获取筛选条件第一列的所有信息
	 * 
	 * @return
	 */
	@Override
	public List<FilterFirst> selectAll(String tenantId) {
		return filterFirstDao.selectAll(tenantId);
	}

	/**
	 * TODO 通过筛选条件的第一列id获取筛选条件的第二列
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public FilterSecondOutDto selectByDto(Integer id) {
		FilterSecondOutDto filterSecondOutDto = new FilterSecondOutDto();
		// 判断filter的类型
		if (id == ConstantCode.FilterFirst.WORK_ORDER_CREATOR_ID
				|| id == ConstantCode.FilterFirst.WORK_ORDER_CURRENT_RECEIVER_ID
				|| id == ConstantCode.FilterFirst.WORK_ORDER_HISTORY_RECEIVER_ID) {
			// 展示2个框
			filterSecondOutDto.setTag(2);
		} else {
			filterSecondOutDto.setTag(1);
		}
		FilterFirst filterFirst = filterFirstDao.selectById(id);
		FilterSecond filterSecond = filterSecondDao.selectById(filterFirst.getFilterSecondId());
		JSONObject filterSecondJson = filterSecond.getContent();
		List<FilterDataVo> data = new ArrayList<>();
		List<TypeVo> typeList = new ArrayList<>();
		JSONArray typeArray = filterSecondJson.getJSONArray(ConstantCode.FilterSecondContentParam.TYPE);
		JSONArray dataArray = filterSecondJson.getJSONArray(ConstantCode.FilterSecondContentParam.DATA);
		if (typeArray == null) {
			for (int i = 0; i < dataArray.size(); i++) {
				FilterDataVo filterData = new FilterDataVo();
				filterData.setId(
						Integer.parseInt(dataArray.getJSONObject(i).get(ConstantCode.FilterSecondContentParam.ID).toString()));
				filterData.setName(dataArray.getJSONObject(i).getString(ConstantCode.FilterSecondContentParam.NAME));
				data.add(filterData);
			}
			filterSecondOutDto.setData(data);
		} else {
			for (int i = 0; i < typeArray.size(); i++) {
				TypeVo type = new TypeVo();
				type.setId(
						typeArray.getJSONObject(i).get(ConstantCode.FilterSecondContentParam.ID).toString());
				type.setName(typeArray.getJSONObject(i).getString(ConstantCode.FilterSecondContentParam.NAME));
				typeList.add(type);
			}
			filterSecondOutDto.setType(typeList);

			for (int i = 0; i < dataArray.size(); i++) {
				FilterDataVo filterData = new FilterDataVo();
				filterData.setId(
						Integer.parseInt(dataArray.getJSONObject(i).get(ConstantCode.FilterSecondContentParam.ID).toString()));
				filterData.setName(dataArray.getJSONObject(i).getString(ConstantCode.FilterSecondContentParam.NAME));
				data.add(filterData);
			}
			filterSecondOutDto.setData(data);

		}
		return filterSecondOutDto;
	}

	/**
	 * TODO 根据id获取筛选条件的第一列
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public FilterFirst selectById(Integer id) {
		return filterFirstDao.selectById(id);
	}
}
